O - Matrix Mathematica Link

Introduction

This notebook shows how to communicate between Mathematica and O-Matrix uses a COM interface.  Note that it requires the file OMServerHelper.dll which is installed in the the same directory as OMServer.dll.

Set Up

The first step is the installation of NETLink

Needs["NETLink`"]

The "Force32Bit" -> True is only necessary on a 64 bit Windows computer.

InstallNET["Force32Bit"->True];

Next, the OMServer and the OMServerHelper COM dlls are loaded.

OMCOM=LoadCOMTypeLibrary["c:\\omwin\\OMServer.dll"]

OMHelperCOM=LoadNETAssembly["c:\\omcom\\OMServerHelper.dll"]

Having loaded the COM dlls we can now instantiate OMServerClass and OMServerHelperClass.  Note that the constructor for the OMServerHelper class takes an instantiation of the OMServer class as an argument.

OMServer=NETNew["OMATRIXSERVERLib.OMServerClass"]

OMServerHelper=NETNew["OMServerHelper", OMServer]

Function Definition

Starting O-Matrix

StartOM opens O - Matrix if it is not already running.  It uses the OMServer method Start.

StartOM[]:=If[ OMServer@Start["OMATRIX"]==1,"Succeeded","Failed"]

Connecting To O-Matrix

Having started O-Matrix, either manually or by using StartOM, it is necessary to connect to O-Matrix using ConnectToOM, which uses the OMServer method Open

ConnectToOM[]:=If[ OMServer@Open[]==1,"Succeeded","Failed"]

Sending a String to O-Matrix For Evaluation

EvalOMExpr takes a string as an argument which it sends to O-Matrix to be evaluated.  It uses the OMServer method Eval.

EvalOMExpr[expr_String]:=If[OMServer@Eval[expr]==1,"Succeeded","Failed"]

Sending a Matrix Of Reals To O-Matrix

Sending a matrix of real numbers to O-Matrix is done in two steps, first sending the matrix using the OMServerHelper function PutData and then sending the name of the matrix with the OMServer command PutMatrix.  OMServerHelper acts an intermediate between Mathematica and OMServer since the OMServer method PutData passes the memory location of the data by passing the first argument of the matrix by reference.  This approach is not currently supported by NETLink.
 arySize is the total number of elements in the matrix.  nr and nc are the number of rows and columns, respectively.  The matrix is transposed before being flattened, since O-Matrix stores matrices in column-order form.

ToOMAsMat[matName_String, mat_?MatrixQ]:=
Module[
{d=Dimensions[mat]},
If[OMServerHelper@PutData[d[[1]]*d[[2]], Flatten[Transpose @ mat],d[[1]],d[[2]]]==1,
If[OMServer@PutMatrix[matName]==1,"Succeeded","PutMatrix Failed"],
"PutData Failed"]
]

Sending a Scalar, Vector or Matrix to O-Matrix

A slower but more flexible approach to transferring data to O-Matrix is to create a string defining the defining the variable and having O-Matrix evaluate that string.  This approach requires a subsidiary function omform which formats the data appropriately.

omform[l_List]:=
Module[{l1,l2},
l1 = ToString /@ l;
l2 = StringReplace[l1,{"{"-> "[", "}" -> "]"}];
ToString[l2]
]

omform[l_]:=ToString[l]

ToOM[varName_String, var_]:=
If[OMServer@Eval[varName<>"="<>omform[var]]==1,"Succeeded","Failed"]

Getting Data From O-Matrix In Matrix Form

FromOMAsMat transfers a variable's value to Mathematica, in matrix form.  It uses the OMServer methods GetMatrix, NRows, NCols and GetVariant.

FromOMAsMat[varname_String]:=
Module[{nr,nc},
OMServer@GetMatrix[varname];
nr = OMServer@NRows[];
nc = OMServer@NCols[];
OMServer@GetVariant[nr*nc]
]

Getting Data From O-Matrix In Actual Form

The O-Matrix functions isnumscalar and isnumvector can be used convert the result returned by FromOMAsMat to their O-Matrix form.  The function IsScalarOrVector tests the variable and returns a result which can be used to convert the variable to its O-Matrix form, as the cost of some extra evaluation time.

IsScalarOrVector[varname_String]:=
Module[{res},
OMServer@Eval["isscalarorvector=int({isnumscalar("<>varname<>"),isnumvector("<>varname<>")})"];
OMServer@GetMatrix["isscalarorvector"];
res =Flatten[ OMServer@GetVariant[2]]
]

FromOM[varname_String]:=
Module[{res, isv},
res=FromOMAsMat[varname];
isv = IsScalarOrVector[varname];
Switch[isv,{0.,0.},res, {0.,1.},res[[All,1]],{1.,0.},res[[1,1]]]
]

Closing O-Matrix

O-Matrix can be closed by evaluating "exit"

StopOM[]:=EvalOMExpr["exit"]

Closing the COM Connection to O-Matrix

The OMServer method Close is used to close the COM connection.

CloseConnection[]:=If[OMServer@Close[]==1,"Succeeded","Failed"]

Example Function Usage

Starting O-Matrix

StartOM[]

Connecting To O-Matrix

ConnectToOM[]

Sending a String to O-Matrix For Evaluation

This example sends the string print "Hello" to O-Matrix, causing Hello to appear in the Command Windows.  Note the use of the escape character \" to define " inside the string.

EvalOMExpr["print \"Hello\" "]

Sending a Matrix Of Reals To O-Matrix

EvalOMExpr can also be used to define variables and functions, as will be illustrated later.

ToOMAsMat["testmat",{{1,2},{3,4},{5,6}}]

Typing testmat into the input window of O-Matrix shows that testmat was transferred successively.

Sending a Scalar to O-Matrix

ToOM["s",3.5]

Getting Data From O-Matrix In Matrix Form

The two previously defined variables can be used to test FromOMAsMat.  Note that s which was sent a scalar, is returned as a one-element matrix.

FromOMAsMat["testmat"]

FromOMAsMat["s"]

Getting Data From O-Matrix In Actual Form

FromOM["testmat"]

FromOM["s"]

Closing O-Matrix

StopOM[]

Closing the COM Connection to O-Matrix

CloseConnection[]

More ComplicatedExamples

Start O-Matrix and the COM Connection

StartOM[]

ConnectToOM[]

Transfer Variables To and From O-Matrix

ToOM["sc",2.5]

FromOMAsMat["sc"]

FromOM["sc"]

ToOM["vec",{1.0, 2.3, 4.5, 5}]

FromOMAsMat["vec"]

FromOM["vec"]

ToOM["mat",{{1.1,2.2},{3.5,4.7},{5.2,6.3}}]

FromOMAsMat["mat"]

FromOM["mat"]

Evaluate O - Matrix Expressions

The O-Matrix function seq is similar to Mathematica's Range

EvalOMExpr["sq=1.5*seq(5)"]

FromOM["sq"]

1.5*Range[5]

The O - Matrix function hilb is similar to the Mathematica function HilbertMatrix

EvalOMExpr["h = hilb(5)"]

FromOMAsMat["h"]//MatrixForm

N[HilbertMatrix[5]]// MatrixForm

Defining and Using O-Matrix Functions

A function which sums the number 1 to n is defined and its execution time is compared to a similar Mathematica function.   "\n" is used to define the separate lines of the function.

EvalOMExpr["clear"]

EvalOMExpr["function f(n) begin \n return sum(1::n) \n end"]

f[n_]:=Plus @@ Range[n]

EvalOMExpr["res = f(10)"];FromOMAsMat["res"]

f[10]

Note the cast of the argument to double since the default floating point type in O-Matrix is single.

AbsoluteTiming @ {EvalOMExpr["res1 = f(double(10^6))"],InputForm @ FromOMAsMat["res1"]}

AbsoluteTiming @ f[(10^6)]

Close O-Matrix and The COM Connection

StopOM[]

CloseConnection[]